package com.lixin.smartclassroom.ui.home.tab4.activity

import androidx.core.widget.doAfterTextChanged
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.addyanxuebaoming
import com.lixin.smartclassroom.api.baomingoption
import com.lixin.smartclassroom.bean.BaoMingDto
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.eventRegistration
import com.lixin.smartclassroom.databinding.ActivityEventRegistrationBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible

/**
 * Description 活动报名
 * Author: lwc
 * Date:2023/12/14 9:00
 */
class EventRegistrationAct : BaseActivity<ActivityEventRegistrationBinding>() {
    override fun getViewBinding() = ActivityEventRegistrationBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "活动报名"
    private var baoMingList = ArrayList<BaoMingDto>()
    private var listBean = ArrayList<DataListBean>()
    private var courseid = ""

    override fun iniView() {
        binding.apply {
            courseid = getIntentString("courseid")
            scopeNetLife {
                val listBean = baomingoption(courseid)
                if (listBean!!.size > 0) {
                    for (i in 0 until listBean.size) {
                        baoMingList.add(BaoMingDto(listBean[i].name!!))
                    }
                    when (listBean.size) {
                        1 -> {
                            ll1.visible()
                            tvName1.text = listBean[0].name
                            etName1.hint = "请输入"+listBean[0].name
                        }

                        2 -> {
                            ll1.visible()
                            ll2.visible()
                            tvName1.text = listBean[0].name
                            tvName2.text = listBean[1].name

                            etName1.hint = "请输入"+listBean[0].name
                            etName2.hint = "请输入"+listBean[1].name
                        }

                        3 -> {
                            ll1.visible()
                            ll2.visible()
                            ll3.visible()
                            tvName1.text = listBean[0].name
                            tvName2.text = listBean[1].name
                            tvName3.text = listBean[2].name
                            etName1.hint = "请输入"+listBean[0].name
                            etName2.hint = "请输入"+listBean[1].name
                            etName3.hint = "请输入"+listBean[2].name
                        }

                        4 -> {
                            ll1.visible()
                            ll2.visible()
                            ll3.visible()
                            ll4.visible()
                            tvName1.text = listBean[0].name
                            tvName2.text = listBean[1].name
                            tvName3.text = listBean[2].name
                            tvName4.text = listBean[3].name

                            etName1.hint = "请输入"+listBean[0].name
                            etName2.hint = "请输入"+listBean[1].name
                            etName3.hint = "请输入"+listBean[2].name
                            etName4.hint = "请输入"+listBean[3].name
                        }
                    }
                }

            }

            receiveEvent<String>("finishAct") {
                finish()
            }
        }

    }


    override fun onClick() {
        binding.apply {
            btnSure.onClick {
                scopeNetLife {
                    val baseBean = addyanxuebaoming(courseid, baoMingList,child_id)
                    if (baseBean.result == "0") {
                        eventRegistration()
                    } else {
                        toast(baseBean.resultNote.toString())
                    }

                }

            }

            etName1.doAfterTextChanged {
                baoMingList[0].content = it.toString()
            }

            etName2.doAfterTextChanged {
                baoMingList[1].content = it.toString()
            }
            etName3.doAfterTextChanged {
                baoMingList[2].content = it.toString()
            }
            etName4.doAfterTextChanged {
                baoMingList[3].content = it.toString()
            }

        }
    }

}